﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>
<?php //check if GET set
	if (!isset($_GET['userid'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
		};
 ?>
<?php //check if svarstymassadistaiid set
	if (!isset($_GET['svarstymassadistaiid'])) {
		$svarstymassadistaiid = 0;  
		}else{
		$svarstymassadistaiid = ($_GET['svarstymassadistaiid']);
		//gaunam paskatinimo detales
		$query = "SELECT * 
			FROM `svarstymassadistai`
			WHERE svarstymassadistaiid=";
		$query .=($_GET['svarstymassadistaiid']);
		$result_set = mysql_query($query, $connection);
		$sabotage = mysql_num_rows($result_set);
		$getsadistai= mysql_fetch_array($result_set);
		//tikrinam ar yra toks paskatinimas ir ar jis priklauso tam klientui
		if(($sabotage==0) or (($getsadistai['userid'])!=($_GET['userid']))){echo "<h1>Sabotage<h1>";};
		
		};
 ?>
<?php //check if user has primary photo

	$query = "SELECT *";
	$query .= " FROM `foto`";
	$query .= " WHERE `foto`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= " AND `pagrindine` = 1";
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);
	$subject = mysql_fetch_array($result_set);
	
	if (($subject['pagrindine']) !=0) {
	$message = "Pagrindine foto jau yra";
	$pagridine_yra = 1;
	$foto_location = $subject['path'];
	$foto_id = $subject['fotoid'];
	
	} else {
	$message = "Pagrindines foto nera";
	$pagridine_yra = 0;
	};
	
	
?>

<?php //Gaunam Pagrindinius kliento duomenys
	
	$query = "SELECT *";
	$query .= " FROM `user`, `lovos`";
	$query .= " WHERE `user`.userid =";
	$query .= "'".$_GET['userid']."'";
	$query .= "AND `user`.lovosid = `lovos`.lovosid";
	
	//echo $query."</br>";
	$result_set = mysql_query($query, $connection);

	$subject = mysql_fetch_array($result_set);

?>

 <?php //procesinam ka gaunam is post
 
if (isset($_POST['submit'])) {
	if($svarstymassadistaiid==0){	
		//naujo pradzia
		$sql = "INSERT INTO `svarstymassadistai` (`userid`, `svarstdata`, `veiksmas`, `burys`, `comment`";
		$sql .= ") VALUES (";
		$sql .= $subject['userid'] . ", ";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= $_POST['veiksmas'] . ", ";
		$sql .= $subject['burys'] . ", ";
		$sql .= "'".$_POST['comment']."'";
		$sql .= ");";
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "summary.php?userid=" . $_GET['userid'];
				redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//naujo pabaiga
	}else{
		//redagavimo pradzhia
		$sql = "UPDATE `svarstymassadistai`";
		$sql .= " SET";
		$sql .= " `userid`=";
		$sql .= $subject['userid'] . ", ";
		$sql .= " `svarstdata`=";
		$sql .= "'".$_POST['svarstymodata']."'" . ", ";
		$sql .= " `veiksmasid`=";
		$sql .= $_POST['veiksmas'] . ", ";
		$sql .= " `burys`=";
		$sql .= $subject['burys'] . ", ";
		$sql .= " `comment`=";
		$sql .= "'".$_POST['comment']."'";
		$sql .= " WHERE `svarstymassadistaiid`=";
		$sql .= $_GET['svarstymassadistaiid'];
		
		$result = mysql_query($sql, $connection);
			if ($result) {
			
	//===========================================================//
	//   HZ kur redirect
	//===========================================================//
			
			
				$to = "form_preprot.php?svarstdata=" . $_POST['svarstymodata'];
				redirect_to($to);
				// Success!
				// $message .= "</br>Client updated Successsullyy!! </br>";
			} else {
				// Display error message.
				$message .= "<p>Client update failed.</p>";
				$message .= "<p>" . mysql_error() . "</p>";
			};
		//redagavimo pabaiga
	};	
		
};
	
  

 ?>


					<article>
						<h2><?php 
							if($svarstymassadistaiid !=0){
								echo "Linkusiu uzpult redagavimas";
							}else{
								echo "Linkusiu uzpult formavimas";
							};
						
						?></h2>
	<?php echo $message."</br>";?>
	<?php //rodomi pagrindiniai kliento duomenys

	//print_r ($subject) ;
	
	//echo "Vardas: ";
	echo $subject['vardas'];
	echo ' ';
	
	// echo "Pavarde: ";
	echo $subject['pavarde'];
	echo ' ';	
	
	//echo "Bylos Nr.: ";
	echo $subject['bylosnr'];
	echo '</br>';
	$tau_dabar = $subject['tautybeid'];
	$pil_dabar = $subject['pilietybeid'];
	
?>
    <?php //rodom pagrindine foto jeigu yra
	if ($pagridine_yra == 1){ 
		echo "</br> <img src=\"";
		echo $foto_location;
		echo "\" width=\"150\">";
	};
?> 
   
<form action="form_sadistai.php?userid=<?php echo ($_GET['userid']);if($svarstymassadistaiid !=0){echo "&svarstymassadistaiid=".$svarstymassadistaiid;};?>" method="post" name="submit">
  
  </br>
  Svarstymo data: <input name="svarstymodata" type="date" value="<?php 
							if($svarstymassadistaiid !=0){
								echo ($getsadistai['svarstdata']);
							}else {echo date("Y-m-j");};
						
						?>" placeholder="1900-12-31" required/>
 <br>
  

<p>
  <label>
    <input type="radio" name="veiksmas" value="2" id="veiksmas_2" <?php 
							if($svarstymassadistaiid !=0){
								if(($getsadistai['veiksmasid'])==2){echo "checked";};
							};
						
						?>/>
    Įrašyti</label>
  <br />
  <label>
    <input type="radio" name="veiksmas" value="1" id="veiksmas_1" <?php 
							if($svarstymassadistaiid !=0){
								if(($getsadistai['veiksmasid'])==1){echo "checked";};
							};
						
						?>/>
    Išrašyti</label>
  <br />
  <label>
    <input type="radio" name="veiksmas" value="0" id="veiksmas_0" <?php 
							if($svarstymassadistaiid !=0){
								if(($getsadistai['veiksmasid'])==0){echo "checked";};
							};
						
						?>/>
    Palikti</label>
  <br><br>
  Comment:<br><textarea name="comment" rows="4" cols="45"> <?php 
							if($svarstymassadistaiid !=0){
								echo ($getsadistai['comment']);
							};
						
						?></textarea><br><br>
<input name="submit" type="submit" value="SubmiNt" />
</form>


			</article>
            
<?php require("includes/footer.php"); ?>
